package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-12-01
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;
    // 统计某一天的注册人数, 生成统计数据

    @Override
    public void registerCount(String day) {
        // 添加记录之前删除表相同日期的数据，在添加一条新数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        // 远程调用得到某一天注册人数
        R registerR = ucenterClient.countRegister(day);

        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        // 把获取数据添加到数据库，
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister); // 注册人数
        statisticsDaily.setDateCalculated(day); // 统计日期

        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(statisticsDaily);
    }

    // 图表显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        // 根据条件查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        // 因为返回两部分数据, 日期和日期对应数量
        // 前端要求数组json结构
        // 创建两个list集合，一个日期list, 一个数量list
        List<String> data_calculatedList = new ArrayList<>(); // 日期
        List<Integer> numDataList = new ArrayList<>(); // 数量

        // 遍历查询所有数据list集合，进行封装
        for (int i = 0; i < staList.size(); i ++ ) {
            StatisticsDaily daily = staList.get(i);
            // 封装日期的list集合
            data_calculatedList.add(daily.getDateCalculated());
            // 封装数量的list集合
            switch (type) {
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("data_calculatedList", data_calculatedList);
        map.put("numDataList", numDataList);
        return map;
    }
}
